package com.fbs.service.impl;

import com.aaa.entity.Permission;
import com.aaa.entity.RRolePermission;
import com.aaa.entity.Role;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbs.dao.EERoleDao;
import com.fbs.dao.EmpInfoDao;
import com.fbs.dao.PermissionDao;
import com.fbs.dao.RRolePermissionDao;
import com.fbs.service.EERoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EERoleServiceImpl implements EERoleService {
    @Autowired
    private EERoleDao eeRoleDao;
    @Autowired
    private EmpInfoDao empInfoDao;
    @Autowired
    private PermissionDao permissionDao;


    @Override
    public Result findAll(String rolenName,String status,Integer currentPage,Integer pageSize) {
        System.out.println("rolenName = " + rolenName);
        System.out.println("status = " + status);

        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        Page<Role> rolePage = new Page<>();
        rolePage.setCurrent(currentPage);
        rolePage.setSize(pageSize);
        if (rolenName != null) {
            wrapper.like("role_name",rolenName);
        }
        if (status!=null){
            wrapper.eq("status",status);
        }
        Page roles = eeRoleDao.selectPage(rolePage,wrapper);
        List records = roles.getRecords();
        long total = roles.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",records);
        map.put("total",total);
        return new Result(200,"查询成功",map);
    }

    @Override
    public Result insert(Role role) {
        int insert = eeRoleDao.insert(role);
        if (insert!=0){
            return new Result(200,"添加成功");
        }
        return new Result(500,"添加失败");
    }

    @Override
    public Result update(Role role) {
        UpdateWrapper<Role> roleUpdateWrapper = new UpdateWrapper<>();
        roleUpdateWrapper.eq("id",role.getId());
        int update = eeRoleDao.update(role, roleUpdateWrapper);
        if (update!=0){
            return new Result(200,"更新成功");
        }
        return new Result(500,"更新失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = eeRoleDao.deleteById(id);
        if (i!=0){
            return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result updateStatrs(String id, String statrs) {
        Integer integer = eeRoleDao.updateStatrs(id, statrs);
        if ("1".equals(statrs)){
            statrs="2";
        }
        if (integer!=0){
            int updatestatus = empInfoDao.updatestatus(statrs, String.valueOf(id));
            return new Result(200,"修改成功");
        }
        return new Result(500,"修改异常");
    }

    @Override
    public Result findPermissionAll(String id) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.isNull("permission_value");
        List<Permission> permissions = permissionDao.selectList(wrapper);
        List<Permission> firsts = firsts(permissions);
        List<Permission> allByRoleId = permissionDao.findAllByRoleId(id);
        ArrayList<Integer> list = new ArrayList<>();
        if (allByRoleId!=null){
            for (Permission permission : allByRoleId) {
                list.add(permission.getId());
            }
        }
        Map<String,Object> map =new HashMap<>();
        map.put("checkItems",list);
        map.put("defaultProps",firsts);
        return new Result(200,"查询成功",map);
    }



    private List<Permission> firsts(List<Permission> permissions){
        //1.一级菜单
        List<Permission> firsts=new ArrayList<>();
        for (Permission permission:permissions){
            System.out.println("permissions = " + permissions);
            if(permission.getPid().equals("0")){
                firsts.add(permission);
            }
        }
        //2.查询一级菜单下的二级菜单
        for(Permission first:firsts){
            List<Permission> children=new ArrayList<>();
            for(Permission p:permissions){
                if(first.getId().equals(Integer.valueOf(p.getPid()))){
                    children.add(p);
                }
            }
            first.setChildren(children);
        }
        return firsts;
    }
}
